package com.study.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;

import java.util.Map;

/**
 * @author ：ihao ∶）
 * @date ：Created in 2021-03-08 15:02
 */
public class ServerAccess {

    /**
     * 请求服务器
     *
     * @param requestUrl   请求地址
     * @param requestParam 请求参数
     * @param clazz        返回定义类
     * @param <T>          泛型
     * @return 响应结果
     * @author 赖浩
     */
    public static <T> T requestPost(String requestUrl, T requestParam, Class<T> clazz) {
        return request(requestUrl, requestParam, clazz, true);
    }

    public static <T> T requestGet(String requestUrl, T requestParam, Class<T> clazz) {
        return request(requestUrl, requestParam, clazz, false);
    }

    private static <T> T request(String requestUrl, T requestParam, Class<T> clazz, boolean isPost) {
        Map<String, Object> map = BeanUtil.beanToMap(requestParam);
        if (isPost) {
            return JSONObject.parseObject(HttpUtil.post(ServerProbe.serverPointProbe() + requestUrl, map), clazz);
        }
        return JSONObject.parseObject(HttpUtil.get(ServerProbe.serverPointProbe() + requestUrl, map), clazz);
    }

}
